package com.example.weatherforcast;

import java.util.ArrayList;
import java.util.List;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationClientOption.AMapLocationMode;
import com.amap.api.location.AMapLocationClientOption.AMapLocationProtocol;
import com.amap.api.location.AMapLocationListener;
import com.example.Custom.LocationCity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.Toast;

public class AddActivity extends Activity implements OnClickListener {
	Button btn_backcity, btn_beijing, btn_tianjin, btn_shanghai, btn_chongqing,
			btn_shenyang, btn_dalian, btn_changchun, btn_zhengzhou, btn_suzhou,
			btn_wuhan, btn_changsha, btn_guangzhou, btn_shenzhen, btn_nanjing,
			btn_search;
	EditText et_city;
	LocationCity btn_locationCity1;
	private List<String> citylist = null;
	SearchView sv_city;
	private String city;
	public static final int CITYRESULT = 0X02;
	private String surecity;
	private AMapLocationClient locationClient = null;
	private AMapLocationClientOption locationOption = new AMapLocationClientOption();
	private SharedPreferences sp = null;
	Editor editor = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addcity);

		initview();
		initdata();
		listen();

	}

	private void initview() {
		// TODO Auto-generated method stub
		btn_backcity = (Button) findViewById(R.id.btn_backcity);
		btn_locationCity1 = (LocationCity) findViewById(R.id.btn_locationCity1);
		btn_beijing = (Button) findViewById(R.id.btn_beijing);
		btn_tianjin = (Button) findViewById(R.id.btn_tianjin);
		btn_shanghai = (Button) findViewById(R.id.btn_shanghai);
		btn_chongqing = (Button) findViewById(R.id.btn_chongqing);
		btn_shenyang = (Button) findViewById(R.id.btn_shenyang);
		btn_dalian = (Button) findViewById(R.id.btn_dalian);
		btn_changchun = (Button) findViewById(R.id.btn_changchun);
		btn_suzhou = (Button) findViewById(R.id.btn_suzhou);
		btn_zhengzhou = (Button) findViewById(R.id.btn_zhengzhou);
		btn_wuhan = (Button) findViewById(R.id.btn_wuhan);
		btn_changsha = (Button) findViewById(R.id.btn_changsha);
		btn_guangzhou = (Button) findViewById(R.id.btn_guangzhou);
		btn_shenzhen = (Button) findViewById(R.id.btn_shenzhen);
		btn_nanjing = (Button) findViewById(R.id.btn_nanjing);
		et_city = (EditText) findViewById(R.id.et_city);
		btn_search = (Button) findViewById(R.id.btn_search);

	}

	private void initdata() {
		// TODO Auto-generated method stub
		citylist = new ArrayList<String>();
		sp = getSharedPreferences("city", MODE_PRIVATE);
		editor = sp.edit();
		int count = sp.getInt("count", -1);
		for (int i = 1; i < count + 1; i++) {
			citylist.add(sp.getString(i + "", "city"));
		}
	}

	private void listen() {
		btn_backcity.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		btn_beijing.setOnClickListener(this);
		btn_tianjin.setOnClickListener(this);
		btn_shanghai.setOnClickListener(this);
		btn_chongqing.setOnClickListener(this);
		btn_shenyang.setOnClickListener(this);
		btn_dalian.setOnClickListener(this);
		btn_changchun.setOnClickListener(this);
		btn_suzhou.setOnClickListener(this);
		btn_zhengzhou.setOnClickListener(this);
		btn_wuhan.setOnClickListener(this);
		btn_changsha.setOnClickListener(this);
		btn_guangzhou.setOnClickListener(this);
		btn_shenzhen.setOnClickListener(this);
		btn_nanjing.setOnClickListener(this);
		btn_search.setOnClickListener(this);
		btn_locationCity1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				initLocation();
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_search:
			city = et_city.getText().toString();
			break;
//		case R.id.btn_backcity:
//			finish();
//			break;
		case R.id.btn_beijing:
			city = btn_beijing.getText().toString();
			break;
		case R.id.btn_tianjin:
			city = btn_tianjin.getText().toString();

			break;

		case R.id.btn_shanghai:
			city = btn_shanghai.getText().toString();
			break;

		case R.id.btn_chongqing:
			city = btn_chongqing.getText().toString();
			break;
		case R.id.btn_shenyang:
			city = btn_shenyang.getText().toString();
			break;
		case R.id.btn_dalian:
			city = btn_dalian.getText().toString();
			break;
		case R.id.btn_changchun:
			city = btn_changchun.getText().toString();
			break;
		case R.id.btn_suzhou:
			city = btn_suzhou.getText().toString();
			break;
		case R.id.btn_zhengzhou:
			city = btn_zhengzhou.getText().toString();
			break;
		case R.id.btn_wuhan:
			city = btn_wuhan.getText().toString();
			break;
		case R.id.btn_changsha:
			city = btn_changsha.getText().toString();

			break;
		case R.id.btn_guangzhou:
			city = btn_guangzhou.getText().toString();
			break;

		case R.id.btn_shenzhen:
			city = btn_shenzhen.getText().toString();
			break;
		case R.id.btn_nanjing:
			city = btn_nanjing.getText().toString();
			break;

		default:
			break;
		}
		// if (city != null) {
		// Intent result = new Intent();
		// result.putExtra("result", city);
		// setResult(CITYRESULT, result);
		// finish();
		// }
		if (city != null && (!citylist.contains(city))) {
			citylist.add(city);
			editor.putString(citylist.size() + "", city);
			editor.commit();
			editor.putInt("count", citylist.size());
			editor.commit();
			finish();
		} else {
			Toast.makeText(this, "你的列表中已有该城市", Toast.LENGTH_SHORT).show();
		}

	}

	/*
	 * 初始化定位
	 * 
	 * @since 2.8.0
	 * 
	 * @author hongming.wang
	 */
	private void initLocation() {
		// 初始化client
		locationClient = new AMapLocationClient(this.getApplicationContext());
		// 设置定位参数
		locationClient.setLocationOption(getDefaultOption());
		startLocation();
		// 设置定位监听
		locationClient.setLocationListener(locationListener);

	}

	/**
	 * 默认的定位参数
	 * 
	 * @since 2.8.0
	 * @author hongming.wang
	 * 
	 */
	private AMapLocationClientOption getDefaultOption() {
		AMapLocationClientOption mOption = new AMapLocationClientOption();
		mOption.setLocationMode(AMapLocationMode.Hight_Accuracy);// 可选，设置定位模式，可选的模式有高精度、仅设备、仅网络。默认为高精度模式
		mOption.setGpsFirst(false);// 可选，设置是否gps优先，只在高精度模式下有效。默认关闭
		mOption.setHttpTimeOut(30000);// 可选，设置网络请求超时时间。默认为30秒。在仅设备模式下无效
		mOption.setInterval(2000);// 可选，设置定位间隔。默认为2秒
		mOption.setNeedAddress(true);// 可选，设置是否返回逆地理地址信息。默认是ture
		mOption.setOnceLocation(false);// 可选，设置是否单次定位。默认是false
		mOption.setOnceLocationLatest(false);// 可选，设置是否等待wifi刷新，默认为false.如果设置为true,会自动变为单次定位，持续定位时不要使用
		AMapLocationClientOption.setLocationProtocol(AMapLocationProtocol.HTTP);// 可选，
																				// 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP
		return mOption;
	}

	/**
	 * 定位监听
	 */
	AMapLocationListener locationListener = new AMapLocationListener() {
		@Override
		public void onLocationChanged(AMapLocation loc) {
			if (null != loc) {
				// 解析定位结果
				if (loc.getErrorCode() == 0) {
					city = loc.getCity();// 城市信息
					// Intent result = new Intent();
					// result.putExtra("result", surecity);
					// setResult(CITYRESULT, result);
					if (city != null && (!citylist.contains(city))) {
						citylist.add(city);
						editor.putString(citylist.size() + "", city);
						editor.commit();
						editor.putInt("count", citylist.size());
						editor.commit();
					}else {
						Toast.makeText(AddActivity.this, "你的列表中已有该城市", Toast.LENGTH_SHORT).show();
					}
					finish();

				}
			} else {
				Toast.makeText(AddActivity.this, "", Toast.LENGTH_SHORT).show();
			}
		}
	};

	/**
	 * 开始定位
	 * 
	 * @since 2.8.0
	 * @author hongming.wang
	 * 
	 */
	private void startLocation() {

		// 设置定位参数
		locationClient.setLocationOption(locationOption);
		// 启动定位
		locationClient.startLocation();
	}

	/**
	 * 停止定位
	 * 
	 * @since 2.8.0
	 * @author hongming.wang
	 * 
	 */
	private void stopLocation() {
		// 停止定位
		if (locationClient != null) {
			locationClient.stopLocation();
		}

	}

	/**
	 * 销毁定位
	 * 
	 * @since 2.8.0
	 * @author hongming.wang
	 * 
	 */
	private void destroyLocation() {
		if (null != locationClient) {
			/**
			 * 如果AMapLocationClient是在当前Activity实例化的，
			 * 在Activity的onDestroy中一定要执行AMapLocationClient的onDestroy
			 */
			locationClient.onDestroy();
			locationClient = null;
			locationOption = null;
		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		stopLocation();
		destroyLocation();
	}
}
